<?php

namespace App\Libraries\Entities;

use Auth;

trait HasCreated
{
    protected $CreatedFlag = MonitoringMode::None;

    public function setCreated()
    {
        if ($this->hasCreatedAt()) {
            $this->setCreatedAt($this->freshTimestamp());
        }
        if ($this->hasCreatedUserId()) {
            $this->setCreatedUserId();
        }
    }

    public function hasCreatedAt()
    {
        $flag = !is_null(static::CREATED_AT) && !$this->isDirty(static::CREATED_AT);
        $flag = $flag && (MonitoringMode::OnlyTime === $this->CreatedFlag || MonitoringMode::All === $this->CreatedFlag);

        return $flag;

        // return !is_null(static::CREATED_AT) && !$this->isDirty(static::CREATED_AT) && $this->useCreated();
    }

    public function useCreated()
    {
        return MonitoringMode::None !== $this->CreatedFlag;
    }

    public function hasCreatedUserId()
    {
        $flag = !is_null(static::CREATED_USER_ID) && !$this->isDirty(static::CREATED_USER_ID);
        $flag = $flag && (MonitoringMode::OnlyUser === $this->CreatedFlag || MonitoringMode::All === $this->CreatedFlag);

        return $flag;

        // return !is_null(static::CREATED_USER_ID) && !$this->isDirty(static::CREATED_USER_ID) && $this->useCreated();
    }

    protected function setCreatedUserId()
    {
        $this->{static::CREATED_USER_ID} = Auth::user() ? Auth::user()->user_id : '';
    }
}
